#!/usr/bin/python3
# -*- coding: UTF-8 -*-
    
#method_1
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# 统计其中的单词出现的个数
import pprint 
   
def count_word():
    f = open(r"news.txt",encoding="utf-8")
    d = {}
    mylist = f.read().strip('.').split()
    mylist2 = list(set(mylist))  #先转换成集合，自动去重，再转换成列表
    for i in mylist2:            #循环遍历列表2
        d[i] = mylist.count(i)
    print(pprint.pformat(sorted(d.items(), key=lambda items:items[1], reverse=True)))

if __name__ == '__main__':
    print(count_word())

#method_2  
import collections

def count_word():
    f = open(r"news.txt",encoding="utf-8")
    f = f.read().strip('.')   #去掉英文结尾的'.'
    a = f.split()    #此处f为list
    print("this is the news word \n", a)
    print("\n", collections.Counter(a), end = '\n')
    
if __name__ == '__main__':
    count_word()
   
 
     
# 思路是对的，但是对标点符号的考虑还不够，因为除了.号，还有? ! , 等各种标点符号。